//
// Created by postgres on 4/17/17.
//
#include "stdio.h"

#include "stdlib.h"
#include "math.h"
#include "time.h"
#include "../include/common/common.h"
#include "../include/02_linear_list/SeqLIst.h"


Status InitList(SeqList *L) {
    L->length = 0;
    return OK;
}

/* 初始条件：顺序线性表L已存在。操作结果：若L为空表，则返回TRUE，否则返回FALSE */
Status ListEmpty(SeqList L) {
    if (L.length == 0)
        return TRUE;
    else
        return FALSE;
}

/* 初始条件：顺序线性表L已存在。操作结果：将L重置为空表 */
Status ClearList(SeqList *L) {
    L->length = 0;
    return OK;
}
/* 初始条件：顺序线性表L已存在。操作结果：返回L中数据元素个数 */
int ListLength(SeqList L)
{
    return L.length;
}
Status GetElem(SeqList L, int i, ElemType *e) {
    if (i < 1 || i > L.length)return ERROR;
    *e = L.list[i - 1];
    return OK;
}

Status LocateElem(SeqList L, ElemType e) {
    if (L.length == 0)return ERROR;
    int i;
    for (i = 0; i < L.length; i++) {
        if (L.list[i] == e)break;
    }
    if (i < L.length) { return i + 1; }
    else { return ERROR; }
}

Status InsertList(SeqList *L, int i, ElemType e) {
    if (i < 1 || i > L->length + 1) {
        fprintf(stderr, "%s\n", "array offset boundary");
        return ERROR;
    }
    if (L->length >= MAXSIZE) {
        fprintf(stderr, "%s\n", "SeqList already full ");
        return ERROR;
    }
    int n;
    if (i <= L->length) {
        for (n = L->length; n >= i; n--) {
            L->list[n] = L->list[n-1];
        }
    }
    L->list[i - 1] = e;
    L->length++;
    return OK;
}
Status add(SeqList *L,ElemType e) {
    if (L->length >= MAXSIZE) {
        fprintf(stderr, "%s\n", "SeqList already full ");
        return ERROR;
    }
    L->list[L->length++] = e;
    return OK;
}
Status ListDelete(SeqList *L, int i, ElemType *e) {
    if (L->length==0)               /* 线性表为空 */
        return ERROR;
    if (i < 1 || i > L->length) {
        fprintf(stderr, "%s\n", "array offset boundary");
        return ERROR;
    }
    int n;
    *e=L->list[i-1];
    for (n =i-1; n < L->length-1; n++) {
            L->list[n] = L->list[n+1];
    }
    L->length--;
    return OK;
}

void SeqList_test() {
    printf("Hello, World!\n");
    SeqList L;
    InitList(&L);
    ElemType e = 8192;
    //InsertList(&L, 1, e);
    //InsertList(&L, 1, 8191);
    //InsertList(&L, 1, 8190);
    add(&L,1);
    add(&L,2);
    add(&L,3);
    add(&L,4);
    ListDelete(&L,1,&e);

}

